﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using UmbracoMedia.Foundation;

using System.Windows.Media.Imaging;
using System.Windows.Data;
using System.ServiceModel;
using USTF.Media.MediaService;
using USTF.Media.SilverlightClient.Foundation;
using USTF.Media.SilverlightClient.ViewModel;
namespace UmbracoMedia
{
    public class UmbracoMediaWrapPanel : WrapPanel
    {
        public void NodeLoaded(object sender, TreeNodeLoadedEventArgs e)
        {

            this.Children.Clear();
            if (e.TreeViewItem.MediaItems != null)
            {
                foreach (MediaItem m in e.TreeViewItem.MediaItems)
                {

                    if (String.Equals(m.Properties.SafeGetValue("ContentTypeAlias", ""), "image", StringComparison.InvariantCultureIgnoreCase))
                    {
                        HierarchicalMediaItem itemView = new HierarchicalMediaItem(m,true);
                        
                        Controls.UmbracoMediaImageControl s = new Controls.UmbracoMediaImageControl(itemView);
                        // s.DataContext = mi;
                        s.ImageEdited += new ImageEditedEventHandler(ImageEdited);
                        this.Children.Add(s);


                    }
                }
            }
        }

        public void ImageEdited(object sender, ImageEditedEventArgs e)
        {

            //BasicHttpBinding bind = new BasicHttpBinding();
            //EndpointAddress endpoint = new EndpointAddress(OpsContext.Current.ServiceBase);

            //mediaServiceSoapClient client = new mediaServiceSoapClient(bind, endpoint);

            //WriteableBitmap b = new WriteableBitmap((int)e.EditedImage.ActualWidth, (int)e.EditedImage.ActualHeight);
            //b.Render(e.EditedImage, new TranslateTransform());

            //byte[] data = OpsContext.GetBase64Jpg(b);
            
            // client.writeContentsAsync(e.MediaItem.MediaCarrier.Id, "test.jpg", Util.GetBase64Jpg(b), Util.Username, Util.Password);

        

        }

       
    }
    
}
